.PHONY: all clean clean_obj lib user_image.S

CCFLAGS	:= -gstabs -I../inc/ -I../lib/ -nostdlib -nostdinc -fno-builtin 

LIBGCC	:= $(shell ${CC} -print-libgcc-file-name)

HEADERS	:=	kernel.h kernel.S.h console.h io.h \
			mem.h pic.h lapic.h ioapic.h sys.h acpi.h intr.h \
			proc.h task.h kpipe.h debug.h syscall.h ipi.h

OBJFILES:= 	kernel.o init.S.o kernel.S.o console.o io.o \
			mem.o pic.o lapic.o ioapic.o sys.o acpi.o intr.o \
			proc.o task.o kpipe.o debug.o syscall.o ipi.o

OBJDUMP := objdump

USER := user-simple

all: kernel kernel.asm

user_image.S:
	${MAKE} -C ../${USER} user_image.S
	cp ../${USER}/user_image.S .

init.S.o: init.S
	${CC} ${CCFLAGS} $< -c -o$@

kernel.S.o: kernel.S user_image.S
	${CC} ${CCFLAGS} $< -c -o$@

%.o: %.c ${HEADERS}
	${CC} ${CCFLAGS} $< -c -o$@

kernel: lib ${OBJFILES}
	${LD} -N -T kernel.ld -o$@ ../lib/*.o ${OBJFILES} ${LIBGCC}

lib:
	${MAKE} -C ../lib

kernel.sym: kernel
	${NM} $< | grep -i " t " | sed "s/ [tT] / /g" > $@

kernel.asm: kernel
	${OBJDUMP} -S $< > $@

clean_obj:
	-${RM} ${OBJFILES}

clean: clean_obj
	-${RM} kernel.sym
	-${RM} kernel.asm
	-${RM} kernel
